package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mysql.cj.util.TimeUtil;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;
    /**
     * 分页查询护理项目
     *
     * @param name
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public PageResponse getByPage(String name,
                                  Integer pageNum,
                                  Integer pageSize,
                                  Integer status) {
        String key = "NursingProject::"+ (name == null?"":name) +
                "_" + pageNum + "_" + pageSize
                + "_" + (status == null? "": status);
        String json = redisTemplate.opsForValue().get(key);
        PageResponse pageResponse = null;
//        使用pagehelper进行分页
        if (StrUtil.isEmpty(json)){
            PageHelper.startPage(pageNum, pageSize);
            Page<NursingProjectVo> p = nursingProjectMapper.getBypage(name,
                    pageNum, pageSize, status);
            pageResponse = new PageResponse(p);
            pageResponse.setRecords(p.getResult());
            redisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(pageResponse),30, TimeUnit.MINUTES);
        }else {
           pageResponse = JSONUtil.toBean(json, PageResponse.class);
        }
        return pageResponse ;
    }

    /**
     * 添加护理项目
     * @param dto
     */
    @Override
    public void add(NursingProjectDto dto) {
        nursingProjectMapper.insert(dto);
        clearCache("NursingProject::*");
    }

    /**
     * 根据id查询回显
     * @param id
     */
    @Override
    public NursingProjectVo getById(Long  id) {
        NursingProjectVo vo=  nursingProjectMapper.getById(id);
        return vo;
    }

    /**
     * 根据id修改
     * @param dto
     */
    @Override
    public void updateById(NursingProjectDto dto) {
        NursingProject bean = BeanUtil.toBean(dto, NursingProject.class);
        nursingProjectMapper.updateById(bean);
        clearCache("NursingProject::*");
    }

    /**
     * 根据状态修改
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(long id, Integer status) {
        nursingProjectMapper.updateStatus(id,status);
        clearCache("NursingProject::*");
    }

    /**
     * 根据id删除
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        nursingProjectMapper.deleteById(id);
        clearCache("NursingProject::*");
    }

    /**
     * 查询所有护理项目
     * @return
     */
    @Override
    public List<NursingProjectVo> list() {
        List<NursingProjectVo> vo=nursingProjectMapper.list();
        return vo;
    }


    //删除缓存
    public void clearCache(String key){
        Set<String> keys = redisTemplate.keys(key);
        redisTemplate.delete(keys);
    }

}
